﻿using System.Linq;
using System.Collections.Generic;
using Eagles.Base;
using Eagles.Base.Cache;
using Eagles.Interface.Core;
using Eagles.Interface.DataAccess;
using Eagles.DomainService.Model.User;
using Eagles.Application.Model.Enums;
using Eagles.Application.Model.Chart.Model;
using Eagles.Application.Model.Chart.Request;
using Eagles.Application.Model.Chart.Response;

namespace Eagles.DomainService.Core
{
    public class ChartHandler : IChartHandler
    {
        private readonly IChartDataAccess dataAccess;
        private readonly ICacheHelper cacheHelper;

        public ChartHandler(IChartDataAccess dataAccess, ICacheHelper cacheHelper)
        {
            this.dataAccess = dataAccess;
            this.cacheHelper = cacheHelper;
        }

        public PieChartResponse GetPieChart(PieChartRequest request)
        {
            var response = new PieChartResponse();
            var tokenInfo = cacheHelper.GetData<TbUserToken>(request.Token);
            switch (request.PieType)
            {
                case Pie.UserSex:
                    var sexInfo = dataAccess.GetUserSexCount(tokenInfo.OrgId);
                    if (sexInfo == null && sexInfo.Count < 1) throw new TransactionException("M01");
                    response.legendData = sexInfo.Select(x => x.Sex).ToArray();
                    response.seriesData = sexInfo.Select(x => new PieSeries { name = x.Sex, value = x.Count }).ToList();
                    break;
                case Pie.UserAge:
                    var ageInfo = dataAccess.GetUserEraCount(tokenInfo.OrgId);
                    if (ageInfo == null && ageInfo.Count < 1) throw new TransactionException("M01");
                    response.legendData = ageInfo.Select(x => x.Era).ToArray();
                    response.seriesData = ageInfo.Select(x => new PieSeries { name = x.Era, value = x.Count }).ToList();
                    break;
                case Pie.UserJoinTime:
                    var joinTimeInfo = dataAccess.GetUserJoinTimeCount(tokenInfo.OrgId);
                    if (joinTimeInfo == null && joinTimeInfo.Count < 1) throw new TransactionException("M01");
                    response.legendData = joinTimeInfo.Select(x => x.JoinTime).ToArray();
                    response.seriesData = joinTimeInfo.Select(x => new PieSeries { name = x.JoinTime, value = x.Count }).ToList();
                    break;
            }
            return response;
        }

        public HistogramResponse GetHistogram(HistogramRequest request)
        {
            var response = new HistogramResponse();
            var tokenInfo = cacheHelper.GetData<TbUserToken>(request.Token);
            switch (request.HistogramType)
            {
                case Histogram.Task:
                    response.legend = new Legend() {data = new[] { "所有任务", "完成任务", "未完成任务"}};
                    var taskX = new List<X>() {new X()};
                    var taskY = new List<Y>() {new Y()};
                    response.xAxis = taskX;
                    response.yAxis = taskY;
                    var taskHisList = new List<HistogramSeries>();
                    var taskList1 = new HistogramSeries();
                    taskList1.name = "所有任务";
                    var taskCount = dataAccess.GetTaskCount(tokenInfo.OrgId, request.BranchId);
                    taskList1.data = new[]
                    {
                        taskCount.January, taskCount.February, taskCount.March, taskCount.April,
                        taskCount.May, taskCount.June, taskCount.July, taskCount.August,
                        taskCount.September, taskCount.October, taskCount.November, taskCount.December
                    };
                    var taskList2 = new HistogramSeries();
                    taskList2.name = "完成任务";
                    var taskCompleteCount = dataAccess.GetTaskCompleteCount(tokenInfo.OrgId, request.BranchId);
                    taskList2.data = new[]
                    {
                        taskCompleteCount.January, taskCompleteCount.February, taskCompleteCount.March, taskCompleteCount.April,
                        taskCompleteCount.May, taskCompleteCount.June, taskCompleteCount.July, taskCompleteCount.August,
                        taskCompleteCount.September, taskCompleteCount.October, taskCompleteCount.November, taskCompleteCount.December
                    };
                    var taskList3 = new HistogramSeries();
                    taskList3.name = "未完成任务";
                    var taskUnCompleteCount = dataAccess.GetTaskUnCompleteCount(tokenInfo.OrgId, request.BranchId);
                    taskList3.data = new[]
                    {
                        taskUnCompleteCount.January, taskUnCompleteCount.February, taskUnCompleteCount.March, taskUnCompleteCount.April,
                        taskUnCompleteCount.May, taskUnCompleteCount.June, taskUnCompleteCount.July, taskUnCompleteCount.August,
                        taskUnCompleteCount.September, taskUnCompleteCount.October, taskUnCompleteCount.November, taskUnCompleteCount.December
                    };
                    taskHisList.Add(taskList1);
                    taskHisList.Add(taskList2);
                    taskHisList.Add(taskList3);
                    response.series = taskHisList;
                    break;
                case Histogram.Activity:
                    response.legend = new Legend() { data = new[] { "所有活动", "完成活动", "未完成活动" } };
                    var activityX = new List<X>() { new X() };
                    var activityY = new List<Y>() { new Y() };
                    response.xAxis = activityX;
                    response.yAxis = activityY;
                    var activityHisList = new List<HistogramSeries>();
                    var activityList1 = new HistogramSeries();
                    activityList1.name = "所有活动";
                    var activityCount = dataAccess.GetActivityCount(tokenInfo.OrgId, tokenInfo.BranchId);
                    activityList1.data = new[]
                    {
                        activityCount.January, activityCount.February, activityCount.March, activityCount.April,
                        activityCount.May, activityCount.June, activityCount.July, activityCount.August,
                        activityCount.September, activityCount.October, activityCount.November, activityCount.December
                    };
                    var activityList2 = new HistogramSeries();
                    activityList2.name = "完成活动";
                    var activityCompleteCount = dataAccess.GetActivityCompleteCount(tokenInfo.OrgId, tokenInfo.BranchId);
                    activityList2.data = new[]
                    {
                        activityCompleteCount.January, activityCompleteCount.February, activityCompleteCount.March, activityCompleteCount.April,
                        activityCompleteCount.May, activityCompleteCount.June, activityCompleteCount.July, activityCompleteCount.August,
                        activityCompleteCount.September, activityCompleteCount.October, activityCompleteCount.November, activityCompleteCount.December
                    };
                    var activityList3 = new HistogramSeries();
                    activityList3.name = "未完成活动";
                    var activityUnCompleteCount = dataAccess.GetActivityUnCompleteCount(tokenInfo.OrgId, tokenInfo.BranchId);
                    activityList3.data = new[]
                    {
                        activityUnCompleteCount.January, activityUnCompleteCount.February, activityUnCompleteCount.March, activityUnCompleteCount.April,
                        activityUnCompleteCount.May, activityUnCompleteCount.June, activityUnCompleteCount.July, activityUnCompleteCount.August,
                        activityUnCompleteCount.September, activityUnCompleteCount.October, activityUnCompleteCount.November, activityUnCompleteCount.December
                    };
                    activityHisList.Add(activityList1);
                    activityHisList.Add(activityList2);
                    activityHisList.Add(activityList3);
                    response.series = activityHisList;
                    break;
            }
            return response;
        }

        public LineChartResponse GetLineChart(LineChartRequest request)
        {
            var response = new LineChartResponse();
            var tokenInfo = cacheHelper.GetData<TbUserToken>(request.Token);
            response.legend = new Legend() { data = new[] { "支部文章" } };
            var taskX = new List<X>() { new X() };
            var taskY = new List<Y>() { new Y() };
            response.xAxis = taskX;
            response.yAxis = taskY;
            var newsLineList = new List<LineSeries>();
            var lineSeries = new LineSeries();
            lineSeries.name = "支部文章";
            var newsCount = dataAccess.TbUserNewsCount(tokenInfo.OrgId, request.BranchId);
            lineSeries.data = new[]
            {
                newsCount.January, newsCount.February, newsCount.March, newsCount.April,
                newsCount.May, newsCount.June, newsCount.July, newsCount.August,
                newsCount.September, newsCount.October, newsCount.November, newsCount.December
            };
            newsLineList.Add(lineSeries);
            response.series = newsLineList;
            return response;
        }

        public TaskActivityPieResponse GetTaskActivityPie(TaskActivityPieRequest request)
        {
            var response = new TaskActivityPieResponse();
            var tokenInfo = cacheHelper.GetData<TbUserToken>(request.Token);
            if (request.PieType == 1)
            {
                var taskInfo = dataAccess.GetTaskCount(tokenInfo.OrgId);
                if (taskInfo == null && taskInfo.Count < 1) throw new TransactionException("M01");
                response.legendData = taskInfo.Select(x => x.BranchName).ToArray();
                response.seriesData = taskInfo.Select(x => new PieSeries { name = x.BranchName, value = x.Count }).ToList();
            }
            else
            {
                var activityInfo = dataAccess.GetActivityCount(tokenInfo.OrgId);
                if (activityInfo == null && activityInfo.Count < 1) throw new TransactionException("M01");
                response.legendData = activityInfo.Select(x => x.BranchName).ToArray();
                response.seriesData = activityInfo.Select(x => new PieSeries { name = x.BranchName, value = x.Count }).ToList();
            }
            return response;
        }
    }
}